##############################
# Analysis of survey data
##############################

rm(list=ls())

library(foreign)
library(Hmisc)
library(ri)
library(stargazer)
library(readstata13)
library(Cairo)
library(svglite)
library("ggplot2")
library(gridExtra)
library(ggpubr)
library(grid)

##############################
# Prepare data
##############################

# load data
d = read.dta13("~/Dropbox/Cerrillos 2017/08_replication/survey_cerrillos_LAPS.dta")
table(d$cuadrante)

# explore data
names(d)
head(d)

# Ideological labels
table(d$a5, exclude = NULL)
d$ideolabels = "no ideology"
d$ideolabels[d$a5<5]="left"
d$ideolabels[d$a5>4 & d$a5 < 7]="center"
d$ideolabels[d$a5>6 & d$a5 < 11]="right"
table(d$ideolabels,d$a5)
prop.table(table(d$ideolabels))

# Ideologia
table(d$a5, exclude = NULL)
prop.table(table(d$a5))
d$ideology = 0
d$ideology[d$a5<11]=1
table(d$ideology)
prop.table(table(d$ideology))

# Interest in politics
table(d$a3, exclude = NULL)
d$interest = 0
d$interest[d$a3<4]=1
table(d$interest)
prop.table(table(d$interest))

# Vote next election
d$a10 = as.numeric(d$a10)
table(d$a10, exclude = NULL)
d$votenextelection = 0
d$votenextelection[d$a10<7]=1
table(d$votenextelection)
round(prop.table(table(d$votenextelection)),2)

# Vote past election
table(d$a11)
d$votepastelection = 0
d$votepastelection[d$a11==1]=1
table(d$votepastelection)
round(prop.table(table(d$votepastelection)),2)

# Likely voter 1
d$likely1 = 0
d$likely1[d$interest==1 & d$votepastelection==1 & d$votenextelection==1]=1
d$likely1[d$interest==1 & d$votepastelection==1 & d$votenextelection==1]=1
prop.table(table(d$likely1))
d$nolikely1 = 1 - d$likely1

# Likely voter 2
d$likely2 = 0
d$likely2[d$votepastelection==1 & d$votenextelection==1]=1
prop.table(table(d$likely2))
d$nolikely2 = 1 - d$likely2

# Likely voter 3
d$likely3 = 0
d$likely3[d$votepastelection==1 & d$interest==1]=1
prop.table(table(d$likely3))
d$nolikely3 = 1 - d$likely3
table(d$likely3,d$b1)

# Ideological labels
prop.table(table(d$ideolabels))

############################################
# Table 4: Ideological and likely voters
############################################

# right-wing and left-wing centrist
d$rightwingcentrist = 0
d$rightwingcentrist[d$a4==2 & d$d2==1 & d$c2==2]=1
d$rightwingcentrist[d$a5<5] = 0
d$rightwingcentrist[d$a5>6 & d$a5<11] = 0
table(d$rightwingcentrist, exclude = NULL)

d$leftwingcentrist = 0
d$leftwingcentrist[d$a4==1 | d$d2==2 & d$c2==2]=1
d$leftwingcentrist[d$a5<5] = 0
d$leftwingcentrist[d$a5>6 & d$a5<11] = 0
table(d$leftwingcentrist, exclude = NULL)

d$ideologicalcentrists = 0
d$ideologicalcentrists[d$rightwingcentrist==1]=1
d$ideologicalcentrists[d$leftwingcentrist==1]=1
table(d$ideologicalcentrists)[2]/(table(d$ideologicalcentrists)[1]+table(d$ideologicalcentrists)[2])
table(d$ideologicalcentrists)[2]/table(d$ideolabels)[1]

d$ideologicalvoters = 0
d$ideologicalvoters[d$ideolabels=="left"] = 1
d$ideologicalvoters[d$ideolabels=="right"] = 1
d$ideologicalvoters[d$rightwingcentrist==1] = 1
d$ideologicalvoters[d$leftwingcentrist==1] = 1
table(d$ideologicalvoters)
prop.table(table(d$ideologicalvoters))

ideo_likely1 = d$likely1[d$ideologicalvoters==1]
nonideo_likely1 = d$likely1[d$ideologicalvoters==0]

table(d$ideolabels,d$likely1)

round(mean(ideo_likely1 ),2)
round(mean(nonideo_likely1),2)
round(mean(ideo_likely1 ),2) - round(mean(nonideo_likely1),3)
t.test(ideo_likely1,nonideo_likely1)

##########################################################
# Table 3: Ideological respondents and policy content
##########################################################

# Non-ideological and Social policies 
prop.table(table(d$ideologicalvoters,d$c1)[1,])

# Ideological and Social policies 
prop.table(table(d$ideologicalvoters,d$c1)[2,])

# Non-ideological and Iron-fist policies
prop.table(table(d$ideologicalvoters,d$c2)[1,])

# Ideological and Iron-fist policies
prop.table(table(d$ideologicalvoters,d$c2)[2,])
